var Construct = {
  activity : {},
  score_card : {},

  edit : function(activity) {
    this.activity = activity;
    var subtype = activity["subtype"];
    if (!subtype) {
      subtype = "atom";
      Activity.editProperty(activity["id"], "subtype", subtype);
    }
    switch (subtype) {
      case "atom"     : this.editAtom(false); break;
      case "molecule" : this.editMolecule(false); break;
      case "reaction" : this.editReaction(false); break;
    }
  },
  
  editAtom : function(save) {
    $("select.molecule, select.reaction, div.construct").hide();
    $("select.atom").show();
    $("#construct_atom").show();
    var ctx = $("#construct_atom canvas.atom")[0].getContext("2d");
    var symbol = $("select.atom")[0].value;
    var x = 0.5 * ctx.canvas.width;
    var y = 0.5 * ctx.canvas.height;
    var scale = 0.25 * ctx.canvas.width;
    ctx.clearRect(0, 0, 4 * scale, 4 * scale);
    new Atom(symbol).drawAtomicModel(ctx, x, y, scale, false, true);
    if (save) {
      Activity.editProperty(this.activity["id"], "atom", symbol);
    }
  },

  editMolecule : function(save) {
    $("select.atom, select.reaction, div.construct").hide();
    $("select.molecule").show();
    $("#construct_molecule").show();
    var ctx = $("#construct_molecule canvas.molecule")[0].getContext("2d");
    var formula = $("select.molecule")[0].value;
    var x = 0.5 * ctx.canvas.width;
    var y = 0.5 * ctx.canvas.height;
    var scale = 0.5 * ctx.canvas.height;
    f2m[formula].drawSpaceFillingModel(ctx, x, y, scale);
    if (save) {
      Activity.editProperty(this.activity["id"], "molecule", formula)
    }
  },

  editReaction : function() {
    $("select.atom").hide();
    $("select.molecule").hide();
    $("select.reaction").show();
    $("#construct_reaction").show();
    var ctx = $("#construct_reaction canvas.reaction")[0].getContext("2d");
  },

  selectAtom : function() {
    $("select.atom").show();
    $("select.molecule").hide();
    $("select.reaction").hide()
  },

  selectMolecule : function() {
    $("select.atom").hide();
    $("select.molecule").show();
    $("select.reaction").hide()
  },

  selectReaction : function() {
    $("select.atom").hide();
    $("select.molecule").hide();
    $("select.reaction").show()
  },

  play : function(activity) {
    this.activity = activity;
    switch (activity["subtype"] != null ? activity["subtype"] : "atom") {
      case "atom" : this.playAtom(); break;
      case "molecule" : this.playMolecule(); break;
      case "reaction" : this.playReaction(); break;
    }
  },

  playAtom : function() {
    var s = this.activity["atom"] ? this.activity["atom"] : "He";
    var a = new Atom(s);
    var construct = $("canvas#construct_atom")[0];
    var x, y, scale;
    if (construct) {
      var ctx = construct.getContext("2d");
      x = 0.5 * construct.width;
      y = 0.5 * construct.height;
      scale = 0.25 * construct.width;
      ctx.clearRect(0, 0, construct.width, construct.height);
      a.drawConstructModel(ctx, x, y, scale, false, true);
    }
    var n = $("canvas.nucleus");
    for (var i = 0; i < n.length; i++) {
      var n_ctx = n[i].getContext("2d");
      var n_x = 0.5 * n_ctx.canvas.width;
      var n_y = 0.5 * n_ctx.canvas.height;
      var n_scale = n_ctx.canvas.width;
      if (s == "He" && i + 1 != n.length) {
        new Atom("Li").drawNucleus(n_ctx, n_x, n_y, n_scale, false, true, i);
      } else if (s == "Li" && i + 1 != n.length) {
        new Atom("He").drawNucleus(n_ctx, n_x, n_y, n_scale, false, true, i);
      } else {
        a.drawNucleus(n_ctx, n_x, n_y, n_scale, false, true, i);
      }
    }
    var e = $("canvas.electrons");
    for (var j = 0; j < e.length; j++) {
      var e_ctx = e[j].getContext("2d");
      var e_x = 0.5 * n_ctx.canvas.width;
      var e_y = 0.5 * n_ctx.canvas.height;
      var e_scale = e_ctx.canvas.width;
      switch (j) {
        case 0 :
          a.drawElectron(e_ctx, e_x, e_y, e_scale, true);
          break;
        case 1 :
          var b = 0.1 * e_scale;
          a.drawElectron(e_ctx, e_x - b, e_y + b, e_scale, true);
          a.drawElectron(e_ctx, e_x + b, e_y - b, e_scale, true);
          break;
        case 2 :
          var c = 0.2 * e_scale;
          a.drawElectron(e_ctx, e_x, e_y + c, e_scale, true);
          a.drawElectron(e_ctx, e_x + c, e_y, e_scale, true);
          a.drawElectron(e_ctx, e_x, e_y - c, e_scale, true);
          a.drawElectron(e_ctx, e_x - c, e_y, e_scale, true);
      }
    }
    dnd.setStartCallBack(function() {} );
    dnd.setDropCallBack(function() {
      var construct = $("canvas#construct_atom")[0];
      construct.ctx = construct.getContext("2d");
      var w = construct.width;
      var h = construct.height;
      construct.ctx.clearRect(0, 0, w, h);
      var scale = 0.25 * w;
      var x = 0.5 * w;
      var y = 0.5 * h;
      var source = $(dnd.source).attr("class");
      if (source == "nucleus") {
        construct_score.nucleus = dnd.value;
      } else if (source == "electrons") {
        construct_score.electrons += Number(dnd.value);
      }
      a.drawConstructModel(construct.ctx, x, y, scale, false, true);
    });
  },

  playMolecule : function() {
    var formula = this.activity["molecule"] ? this.activity["molecule"] : "HCl";
    var molecule = f2m[formula];
    var construct = $("canvas#construct_molecule")[0];
    var x, y, w, h, scale;
    if (construct) {
      construct.ctx = construct.getContext("2d");
      x = 0.5 * construct.width;
      y = 0.5 * construct.height;
      scale = 0.5 * construct.height;
      construct.ctx.clearRect(0, 0, construct.width, construct.height);
      construct_score.atoms = [];
      molecule.drawConstructModel(construct.ctx, x, y, scale, 2);
      var symbols = ["H","He","Na","C","O","Cl"];
      var atoms = $("canvas.atom");
      for (var i = 0; i < atoms.length || i < symbols.length; i++) {
        var ctx = atoms[i].getContext("2d");
        ctx.clearRect(0, 0, atoms[i].width, atoms[i].height);
        $(atoms[i]).attr("data-value", symbols[i]);
        x = 0.5 * atoms[i].width;
        y = 0.5 * atoms[i].height;
        var e = new Atom(symbols[i]);
        e.drawAtom(ctx, x, y, scale);
      }
    }
    dnd.setStartCallBack(function() {
      
    });
    dnd.setDropCallBack(function() {
      if (construct_score.atoms.length < 2) {
        construct_score.atoms.push(dnd.value);
        var construct = $("canvas#construct_molecule")[0];
        construct.ctx = construct.getContext("2d");
        var x = 0.5 * construct.width;
        var y = 0.5 * construct.height;
        var scale = 0.5 * construct.height;
        f2m["HCl"].drawConstructModel(construct.ctx, x, y, scale, 2);
      }
    })
  },

  playReaction : function() {

  },

  score : function() {
    switch (this.activity["subtype"]) {
      case "atom" :
        var i = construct_score.nucleus;
        var j = construct_score.electrons;
        if (i == 4 && j == s2e[this.activity["atom"]].valence) {
          alert("You passed the activity.");
        } else {
          alert("Try again.");
          construct_score = { "electrons" : 0 };
          this.reset();
        }
        break;
      case "molecule" :
        var atoms = construct_score.atoms;
        if (($.inArray("H", atoms) > -1 && $.inArray("Cl", atoms) > -1) ||
          ($.inArray("Na", atoms) > -1 && $.inArray("Cl", atoms) > -1) ||
          ($.inArray("C", atoms) > -1 && $.inArray("O", atoms) > - 1)) {
          alert("You passed the activity.");
        } else {
          alert("Try again.");
          construct_score = { "atoms" : [] };
          this.reset();
        }
        break;
      case "reaction" :

        break;
    }
  },

  reset : function() {
    if (this.activity) {
      construct_score = { atoms : [], electrons : 0 };
      this.play(this.activity);
    }
  },

  changeSubtype : function(subtype) {
    Activity.editProperty(this.activity["id"], "subtype", subtype);
    if (subtype == "atom") {
      this.editAtom();
    } else if (subtype == "molecule") {
      this.editMolecule();
    } else if (subtype == "reaction") {
      this.editReaction();
    }
  }
};

var construct_score = {
  atoms : [],
  electrons : 0
};